package com.wq.gamesnack.utils;

import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Vibrator;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

/**
 * Created by WQ on 2018/4/13.
 */

public class AnimTool {

    public static final  int DEF_VIBRATE_DURATION=10;

    public static void singleVibrate(Context context, int millisecond) {
        try {
            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(millisecond);
        }catch (Exception e){e.printStackTrace();}
    }
    /**
     * 按鈕點擊動畫效果
     *
     * @param view
     */
    public static void showScalAnim(final View view) {
        final ValueAnimator valueAnimator = ValueAnimator.ofFloat(1f, 1.2f, 1f);
        valueAnimator.setDuration(200);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animatedValue = (float) animation.getAnimatedValue();
                view.setScaleX(animatedValue);
                view.setScaleY(animatedValue);
            }
        });
        valueAnimator.start();
    }
    /**
     * 按鈕點擊動畫效果
     *
     * @param view
     */
    public static void showRoateAnim(final View view) {
        //创建旋转动画
        Animation animation = new RotateAnimation(0, 359,view.getWidth()/2,view.getHeight()/2);
        animation.setDuration(300);
        animation.setInterpolator(new AccelerateInterpolator());
//        animation.setRepeatCount(1);//动画的重复次数
        animation.setFillAfter(true);//设置为true，动画转化结束后被应用
        view.startAnimation(animation);//开始动画
//        final ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 360);
//        valueAnimator.setDuration(500);
//        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                float animatedValue = (float) animation.getAnimatedValue();
//                view.setPivotX(view.getWidth()/2);
//                view.setPivotY(view.getHeight()/2);
//                view.setRotation(animatedValue);
//            }
//        });
//        valueAnimator.start();
    }
}
